
/*---------------------------------------------------------------------------

  FILENAME:
        buf2serial.c

  PURPOSE:
	Pipe cmd from queue to serial.

  REVISION HISTORY:
        Date            Engineer        Revision        Remarks
	16/02/2009	Vincent. Young	1.2	buf2serial

  NOTES:


  LICENSE:
        Copyright (c) 2009, Vincent Young (vincentgodyoung@hotmail.com)

        This source code is released for free distribution under the terms
        of the GNU General Public License.

----------------------------------------------------------------------------*/

// local project include files
#include "cmdbufqueue.h"
#include "serial.h"

#define LOGREGMODE FALSE

// 从命令队列里取出命令发送到串口

void * buf2serial()
{
	int len;
	socketid = 0;
	while (1)
	{
		if (firstcmd == NULL)
			continue;
		//把第一个命令发出去
		if (firstcmd->cmd == NULL)
		{
			printf("buf2serial: firstcmd none!\n");
			cmddelfirst();
			continue;
		}
#if LOGREGMODE
		printf("buf2serial: writing firstcmd[%s] cmdlength[%d]\n", (char*)firstcmd->cmd, firstcmd->cmdlength);
#endif
		len = SerialWrite((char *)firstcmd->cmd, firstcmd->cmdlength);
		if (len < 0)
		{
			printf("buf2serial: write error [%d]\n", len);
			cmddelfirst();	//
			return NULL;
		}
#if	LOGREGMODE
		printf("buf2serial: cmd [%s] length [%d] send\n", (char *)(firstcmd->cmd), firstcmd->cmdlength);
#endif
		//记录发送命令属于哪一个socketid
		socketid = firstcmd->socketid;
		//删除第一个命令
		cmddelfirst();
	}
	return NULL;
}
